'''
Created on Jun 20, 2012

@author: thomas_dao
'''
from web import base
from web.base import BaseHandler
from web import urls
from web import util
from entity.entity import Bill
import json
import logging
from google.appengine.ext import ndb
from app import bill_async

class HomePageHandler(BaseHandler):
    @ndb.synctasklet
    @base.user_required
    def get(self):
        template = util.jinja_environment().get_template('home.html')
        values = {}
        user_key = util.get_current_user_key()
        (user, 
         created_bills, 
         requested_bills) = yield (util.get_current_user(), 
                                   bill_async.my_created_bills(user_key), 
                                   bill_async.my_requested_bills(user_key))
         
        values['user'] = json.dumps(util.ndb_to_dict(user))
        values['created_bills'] = json.dumps(created_bills)
        values['requested_bills'] = json.dumps(requested_bills)
        self.response.out.write(template.render(values))    
app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()